package com.example.idie.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.idie.bean.User;
import com.example.idie.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
@Slf4j
public class UserLoginService {
    @Resource
    private UserMapper userMapper;

    public HashMap validateLogin(String username, String password, Integer usertype) {
        HashMap<String, Object> response = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        User user = this.findUserByName(username);
        System.out.println(user);
        if (user == null) {
            responseData.put("token", -1);
            response.put("code", 0);
            response.put("msg", "用户名不存在！");
            response.put("data", responseData);
        } else {
            if (user.validatePassword(password) && user.validateUserType(usertype)) {
                responseData.put("token", username + "-login");
                responseData.put("loginTime", System.currentTimeMillis());
                response.put("code", 1000);
                response.put("msg", "登录成功");
                response.put("data", responseData);
            } else {
                responseData.put("token", -1);
                response.put("code", -1);
                response.put("msg", "用户名或密码不正确！");
                response.put("data", responseData);
            }
        }
        return response;
    }

    public HashMap getInfo(Integer userId) {
        HashMap<String, Object> response = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        User user = this.findUserById(userId);
        responseData.put("username", user.getUsername());
        response.put("code", 1000);//前端所需的状态码
        response.put("message", "登录成功");
        response.put("data", responseData);
        return response;
    }

    public HashMap changePassword(String username, String oldPassword, String newPassword) {
        HashMap<String, Object> response = new HashMap<>();
        User user = this.findUserByName(username);
        if (user != null && user.validatePassword(oldPassword)) {
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("username", username);
            updateWrapper.set("password", newPassword);
            if (userMapper.update(null, updateWrapper) == 0) {
                response.put("code", -1);//前端所需的状态码
                response.put("message", "操作失败，请联系管理员");
            } else {
                response.put("code", 1000);//前端所需的状态码
                response.put("message", "密码修改成功，请重新登录");
            }
        } else {
            response.put("code", -1);//前端所需的状态码
            response.put("message", "用户名或原密码不正确！");
        }
        return response;
    }

    public User findUserByName(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }

    public User findUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    public HashMap register(String username,String password, Integer usertype) {
        HashMap<String, Object> response = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        User existUser = userMapper.selectOne(queryWrapper);
        if(existUser != null){
            response.put("code", -1);
            response.put("message", "用户名已注册，请重新注册");
        }else{
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setUserType((usertype));
            userMapper.insert(user);
            response.put("code", 1000);
            response.put("message", "注册成功，请重新登录");
        }
        return response;

    }
}

